package src.edu.wayne.cs.csc4110.hw6.implementations.userPanels;

import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

import src.edu.wayne.cs.csc4110.hw6.implementations.VideoFrameImpl;
import src.edu.wayne.cs.csc4110.hw6.implementations.stateMachine.StateMachine;
import src.edu.wayne.cs.csc4110.hw6.interfaces.User;

public class UserLoginPanel extends StateMachinePanel {
	/**
	 * 
	 */
	private static final long serialVersionUID = -3873968149804844746L;
	
	JTextField jtfEmail = new JTextField();
	JTextField jtfUserName = new JTextField();
	JPasswordField jtpfUserPassword = new JPasswordField();

	public UserLoginPanel(StateMachine stateMachine){
		super(stateMachine);
		//Create a panel
		this.setLayout(new BorderLayout(0, 175));
		this.setBackground(VideoFrameImpl.getColor());
		this.setForeground(VideoFrameImpl.getColor());
		this.add(new JLabel("Welcome to Facelook.  Please Login"), BorderLayout.NORTH);
		
		//Put buttons on it
		JPanel buttonPanel = new JPanel();
		buttonPanel.setLayout(new GridLayout(1,0));
		buttonPanel.setBackground(VideoFrameImpl.getColor());
		
		buttonPanel.add(getRegisterButton());
		buttonPanel.add(getLoginButton());
		buttonPanel.add(getCancelButton());
		
		this.add(buttonPanel, BorderLayout.SOUTH);

		JPanel centerPanel = new JPanel();
		centerPanel.setLayout(new GridLayout(0,1,2,2));
		centerPanel.setBackground(VideoFrameImpl.getColor());
		centerPanel.add( getTheEmailTextField() );	
		centerPanel.add( getUserPasswordTextField() );
		
		this.add(centerPanel, BorderLayout.CENTER);

		stateMachine.setDisplayPanel(this, "Login");
	}
	
	private void goToRegisterState() {
		stateMachine.setState(stateMachine.getRegisterState());
		stateMachine.goToRegisterState();
	}
	
	private void goToLoggedInState(){
		if (checkLogin())
		{		
			User user = getStateMachine().getUser(strEmail);
			getStateMachine().setCurrentUser(user);
			getStateMachine().setState(getStateMachine().getLoggedInState());
		}
		getStateMachine().goToLoggedInState();
	}
	
	private JButton getRegisterButton(){
		JButton jbtnRegister = new JButton("Register New User");
		jbtnRegister.addActionListener(
				new ActionListener() 
				{
					public void actionPerformed(ActionEvent e)
					{
						goToRegisterState();
					}
				}
			);
		return jbtnRegister;
	}
	
	private JButton getLoginButton(){
		JButton jbtnSubmitLogin = new JButton("Login");
		jbtnSubmitLogin.addActionListener(
    			new ActionListener() 
    			{
    				public void actionPerformed(ActionEvent e)
    				{
    					if (checkLogin())
    					{
    						goToLoggedInState();
    					}
    					else
    					{
    						JOptionPane.showMessageDialog(null, "Login Failed. Please Try Again");
    					}
    				}
    			}
    		);  
		
		return jbtnSubmitLogin;

	}
	
	private JButton getCancelButton(){
		JButton jbtnCancel = new JButton("Cancel");
		jbtnCancel.addActionListener(
    			new ActionListener() 
    			{
    				public void actionPerformed(ActionEvent e)
    				{
    					if (getStateMachine().getCurrentUser() != null)
    					{
    						cancelLogin();
    					}
    				}
    			}
    		);  
		return jbtnCancel;
	}
	
	private JPanel getTheEmailTextField(){
		JPanel emailTextField = new JPanel();
		emailTextField.setLayout(new GridLayout(1,0,2,2));
		emailTextField.add(new JLabel("Email Address:"));
		emailTextField.add(jtfEmail);

		jtfEmail.addFocusListener(

			new FocusListener()
			{
				@Override
				public void focusLost(FocusEvent e)
				{
					strEmail = jtfEmail.getText();
				}

				@Override
				public void focusGained(FocusEvent arg0) 
				{
					//Nothing to do here.
				}
			}
		);
		
		jtfEmail.addActionListener(
			new ActionListener() 
			{
				public void actionPerformed(ActionEvent e)
				{
					strEmail = jtfEmail.getText();
				}
			}
		);
		
		return emailTextField;
	
	}
	
	public JPanel getUserPasswordTextField(){
		JPanel userPassWordTextField = new JPanel();
		userPassWordTextField.setLayout(new GridLayout(1,0,2,2));
		userPassWordTextField.add(new JLabel("Password:"));
		userPassWordTextField.add(jtpfUserPassword);

		jtpfUserPassword.addFocusListener(

			new FocusListener()
			{
				@Override
				public void focusLost(FocusEvent e)
				{
					strUserPassword = new String(jtpfUserPassword.getPassword());
				}

				@Override
				public void focusGained(FocusEvent arg0) {
					
				}
			}
		);
		
		jtpfUserPassword.addActionListener(
			new ActionListener() 
			{
				public void actionPerformed(ActionEvent e)
				{
					strUserPassword = new String(jtpfUserPassword.getPassword());
				}
			}
		);
		
		return userPassWordTextField;
	}
	
	private void cancelLogin()
	{
		if (stateMachine.getCurrentUser() != null)
		{
			super.strEmail = stateMachine.getCurrentUser().getEmail();
			super.strUserPassword = stateMachine.getCurrentUser().getPassword();
			stateMachine.setState(stateMachine.getLoggedInState());
			stateMachine.goToLoggedInState();
		}
	}
}
